package com.samoiloff.ui.btns {
	import caurina.transitions.Tweener;

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author Samoiloff (http://blog.samoiloff.com)
	 */
	public class BrightBtnController extends EventDispatcher {
		
		protected var _mc		: MovieClip;
		protected var _hitMc	: MovieClip;
		private var _enabled: Boolean;
		
		private var _paleOnDisable:Boolean;

		public function BrightBtnController(mc: MovieClip, paleOnDisable:Boolean = true) {
			
			_paleOnDisable = paleOnDisable;
			
			_mc = mc;
			_hitMc = mc.hitMc ? MovieClip(mc.hitMc) : mc;
			if (_hitMc != _mc) {
				_mc.mouseEnabled = false;
			}
			
			_hitMc.useHandCursor = _hitMc.buttonMode = true;
			
			_hitMc.addEventListener(MouseEvent.MOUSE_DOWN, transitEvent);
			_hitMc.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			_hitMc.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_hitMc.addEventListener(MouseEvent.CLICK, onClick);
		}

		public function destroy():void {
			_hitMc.removeEventListener(MouseEvent.MOUSE_DOWN, transitEvent);
			_hitMc.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			_hitMc.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_hitMc.removeEventListener(MouseEvent.CLICK, onClick);
			
			_hitMc = null;
			_mc = null;
		}

		protected function onMouseOver(e:MouseEvent): void {
			//Tweener.addTween(_mc, {time:.5, transition:"easeOutCubic", _brightness:.3});
			var mtx: Array = [2, 0, 0, 0, -63.5, 0, 2, 0, 0, -63.5, 0, 0, 2, 0, -63.5, 0, 0, 0, 1, 0];
			Tweener.addTween(_mc.body, {_ColorMatrix_matrix:mtx, time:0.5 });
			dispatchEvent(e);
		}

		protected function onMouseOut(e:MouseEvent): void {
			//Tweener.addTween(_mc, {time:.5, transition:"easeOutCubic", _brightness:0});
			var mtx: Array = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0];
			Tweener.addTween(_mc.body, { _ColorMatrix_matrix:mtx, time:0.5 });
			dispatchEvent(e);
		}

		private function transitEvent(e:Event): void {
			dispatchEvent(e);
		}
		
		protected function onClick(e:MouseEvent): void {
			transitEvent(e);
		}

		
		public function get hitMc(): MovieClip {
			return _hitMc;
		}
		
		public function get mc(): MovieClip {
			return _mc;
		}
		
		public function get enabled(): Boolean {
			return _enabled;
		}
		
		public function set enabled(enabled: Boolean): void {
			_enabled = enabled;
			_hitMc.mouseEnabled = _hitMc.mouseChildren = enabled;
			if(_paleOnDisable){
				Tweener.addTween(_mc, {_saturation:_enabled ? 1 : 0, alpha: _enabled ? 1 : .8 });
			}
		}
	}
}
